#include "stdafx.h"
#include "Player.h"
#include "InputManager.h"
#include "EntityManager.h"
#include "EntityFactory.h"
#include "GameGUI.h"
#include "Entity.h"
#include "Grid.h"


Player::Player(InputManager* input_manager, EntityManager* entity_manager, EntityFactory* entity_factory, GameGUI* game_gui, Heightmap::Vertex* terrain_vertices, Grid* grid)
	: m_input_manager(input_manager), m_entity_manager(entity_manager), m_entity_factory(entity_factory), m_game_gui(game_gui), m_terrain_vertices(terrain_vertices), m_selected_tower(ESelectedTower::None),
	m_gold(500), BasicTowerPrice(80), SlowingTowerPrice(125), m_grid(grid)
{
	m_raycast_hit = Vector3(0.0f, 0.0f, 0.0f);
}


Player::~Player(void)
{
}

void Player::AddGold(unsigned int value)
{
	m_gold += value;
}

bool Player::PurchaseTower()
{
	switch (m_selected_tower)
	{
	case ESelectedTower::None:
		break;
		
	case ESelectedTower::BasicTower:
		{
			if (m_gold >= BasicTowerPrice)
			{
				// Create tower from entity factory
				Entity* ent = m_entity_factory->Create(EEntityType::Basic_Tower, m_grid);
				ent->SetPosition(m_raycast_hit);
				m_entity_manager->AddEntity(ent);

				// Adjust gold
				m_gold -= BasicTowerPrice;

				// Reset selected tower
				m_selected_tower = ESelectedTower::None;

				return true;
			}
			else
			{
				m_selected_tower = ESelectedTower::None;
				break;
			}
		}
	case ESelectedTower::SlowingTower:
		{
			if (m_gold >= SlowingTowerPrice)
			{
				// Create tower from entity factory
				Entity* ent = m_entity_factory->Create(EEntityType::Slow_Tower, m_grid);
				ent->SetPosition(m_raycast_hit);
				m_entity_manager->AddEntity(ent);

				// Adjust gold
				m_gold -= SlowingTowerPrice;

				// Reset selected tower
				m_selected_tower = ESelectedTower::None;

				return true;
			}
			else
			{
				m_selected_tower = ESelectedTower::None;
				break;
			}
		}
	};


	return false;
}

void Player::Update(Vector3 hit)
{
	m_game_gui->GetTextfield(ETextfield_Type::GOLD_COUNTER)->SetText("GOLD: " + ToString(m_gold));
	m_raycast_hit = hit;


	// Check for left click
	if (m_input_manager->ButtonPressed(EMouseButton::MB_Left))
	{

		Vector2 mouse_position(m_input_manager->m_mouse.m_position.x, m_input_manager->m_mouse.m_position.y);

		// Check if mouse is over any button
		Gui::Button* btn = m_game_gui->GetButton(mouse_position.x, mouse_position.y);
		if (btn)
		{
			switch (btn->GetButtonType())
			{
			case Gui::EButtonType::BASIC_TOWER_SELECT:
				{
					m_selected_tower = ESelectedTower::BasicTower;
				}
				break;

			case Gui::EButtonType::SLOWING_TOWER_SELECT:
				{
					m_selected_tower = ESelectedTower::SlowingTower;
				}
				break;
			}
		}
		else
		{
			Grid::Square& square = m_grid->GetSquare(hit);

			if (square.m_state == Grid::ESquareState::free)
			{
				if (PurchaseTower())
					square.m_state = Grid::ESquareState::occupied;
			}
			else
				m_selected_tower = ESelectedTower::None;
		}
	}
}
